package com.example.brage;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import io.flutter.Log;

public class BadgeUtils {
    /// 创建一个Tag
    private static final String TAG = "BadgeUtils";
    /// 创建一个channel_id
    private static final String NOTIFICATION_CHANNEL_ID = "default_notification_channel_id";
    // 通知id
    private static final int NOTIFICATION_ID = 1001;

    public static void  setBadgeCount(Context context, int badgeCount) {
        /// 获取制造产商
        if(Build.MANUFACTURER.equalsIgnoreCase("xiaomi")){
            // 小米设备需要使用自带的 BadgeUtils 设置角标数
            setBadgeCountForXiaomi(context, badgeCount);
        }else if(Build.MANUFACTURER.equalsIgnoreCase("huawei")){
            // 华为设备需要使用华为提供的 BadgeProvider 设置角标数
            setBadgeCountForHuawei(context, badgeCount);
        }else {
            // 其他设备需要使用 Notification 设置角标数
            setBadgeCountForOtherDevices(context, badgeCount);
        }
    }

    private  static  void setBadgeCountForXiaomi(Context context, int badgeCount){
        try {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Badge Channel", NotificationManager.IMPORTANCE_LOW);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationManager.createNotificationChannel(channel);
            }
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                    .setData(Uri.parse("package:" + context.getPackageName()));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            Notification notification = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                notification = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
                        .setContentTitle("Badge Count")
                        .setContentText(String.valueOf(badgeCount))
                        .setSmallIcon(R.drawable.ic_notification)
                        .setLargeIcon(getAppIconBitmap(context))
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .build();
            }

            notificationManager.notify(NOTIFICATION_ID, notification);
        }catch (Exception e) {
            Log.e(TAG, "setBadgeCountForXiaomi: " + e.getMessage());
        }
    }

    private static void setBadgeCountForHuawei(Context context, int badgeCount){
        try{
            Bundle extra = new Bundle();
            extra.putString("package", context.getPackageName()); // 应用包名
            extra.putString("class", getMainActivityClassName(context)); // 应用入口 Activity 类名
            extra.putInt("badgenumber", badgeCount); // 角标数
            context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, extra);
        }catch (Exception e){
            Log.e(TAG, "setBadgeCountForHuawei: " + e.getMessage());
        }
    }

    private static void setBadgeCountForOtherDevices(Context context, int badgeCount){
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Badge Channel", NotificationManager.IMPORTANCE_LOW);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannel(channel);
        }

        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
                    .setContentTitle("Badge Count")
                    .setContentText(String.valueOf(badgeCount))
                    .setSmallIcon(R.drawable.ic_notification)
                    .setLargeIcon(getAppIconBitmap(context))
                    .setAutoCancel(true)
                    .build();
        }

        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    private static Bitmap getAppIconBitmap(Context context){
        PackageManager packageManager = context.getPackageManager();
        try {
            return BitmapFactory.decodeResource(packageManager.getResourcesForApplication(context.getPackageName()),
                    packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).icon);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "getAppIconBitmap: " + e.getMessage());
        }
        return null;
    }

    private static String getMainActivityClassName(Context context) {
        PackageManager pm = context.getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
        if (intent != null) {
            return intent.getComponent().getClassName();
        }
        return "";
    }
}
